#include <arch/philips/lpc2103.h>
#include "sys.h"
#include "ui.h"

void spi_port_init()
{
	// IMPORTANT THIS COMMAND MUST GO FIRST
	spi_on(CS0);
	spi_send(0x40);// write the expander
	spi_send(0x0A);//BANK BYTE
	spi_send(0xFA);
	spi_off(CS0);
	// END IMPORTANT

	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x00);//IODIRA
	spi_send(0xF);//Input pins!
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x02);//GPINTENDA
	spi_send(0x00);//Enable ints on all pins
	spi_off(CS0);
	

	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x10);//IODIRB
	spi_send(0x0);//Outputs!
	spi_off(CS0);

	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x01);//IPOLA
	spi_send(0x00);
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x03);//DEFVALA
	spi_send(0x00);//0's
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x04);//INTCONA
	spi_send(0xFF);//Compare against DEFVALA
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x12);//GPINTENB
	spi_send(0x00);//DISABLE
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x06);
	spi_send(0x80);
	spi_off(CS0);
	
	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x1A);
	spi_send(0xFF);
	spi_off(CS0);
	
	led_pulse(100);
	led_pulse(100);
	led_pulse(100);
	
	spi_led(~(0x3));
	//delay_ms_t0(500);
	

}

void spi_menu(int *op1, int *op2, int *op3, int *op4)
{
	int testdata = 0;
	int current = 0;
	int current2[4] = {0,0,0,0};

	int selectmenu = 0;
	
	int led_mode = 1;
	int debounce = 0;
	int led_left[4] = {1,4,16,64};
	int led_right[4] = {2,8,32,128};





while (1){
		
		
	
		spi_on(CS0);
		spi_send(0x41);
		spi_send(0x09);
		testdata = spi_send(0x0);
		spi_off(CS0);
		
		/*if (testdata != 0 && testdata != 255 && testdata >= 100){
		uart0_puts(int2str(testdata));
		uart0_puts("\n");
		}*/
		
		if (testdata == 0x1){
		delay_ms_t0(200);
				debounce = 1;
			if (led_mode == 1){
			selectmenu++;
			} else if (led_mode == 2) {
			
			current2[selectmenu]++;
			}
			if (selectmenu == 4)
				selectmenu = 0;
			if (current2[selectmenu] == 4)
				current2[selectmenu] = 0;
		spi_led(~led_left[selectmenu] & ~led_right[current2[selectmenu]]);
		}
		
		
		if (testdata == 0x2)
		{
		delay_ms_t0(200);
				debounce = 1;
			if (led_mode == 1){
			selectmenu--;
			} else if (led_mode == 2) {
			
			current2[selectmenu]--;
			}
			if (selectmenu == -1)
				selectmenu = 3;
			if (current2[selectmenu] == -1)
				current2[selectmenu] = 3;
		spi_led(~led_left[selectmenu] & ~led_right[current2[selectmenu]]);
		}
		if (testdata == 0x4)
		{
		delay_ms_t0(200);
			debounce = 1;
			if (led_mode == 1) {
				led_mode = 2;
				spi_led(~led_left[selectmenu] & led_right[current2[selectmenu]]);
				delay_ms_t0(200);
				spi_led(~led_left[selectmenu] & ~led_right[current2[selectmenu]]);
			} else {
				led_mode = 1;
				spi_led(led_left[selectmenu] & ~led_right[current2[selectmenu]]);
				delay_ms_t0(200);
				spi_led(~led_left[selectmenu] & ~led_right[current2[selectmenu]]);
			}
		}
		
		if (testdata == 0x8)
		{
			spi_led(~(0x3));
			delay_ms_t0(300);
			spi_led(~(0xC));
			delay_ms_t0(300);
			spi_led(~(0x30));
			delay_ms_t0(300);
			spi_led(~(0xC0));
			delay_ms_t0(300);
			spi_led(~led_left[selectmenu] & ~led_right[current2[selectmenu]]);
			
			*op1 = current2[0];
			*op2 = current2[1];
			*op3 = current2[2];
			*op4 = current2[3];
			return;
		}
		
		
		
		
		
	}

	return;

}

void spi_led(int led_Out)
{

	spi_on(CS0);
	spi_send(0x40);
	spi_send(0x1A);
	spi_send(led_Out);
	spi_off(CS0);

}


